<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Java-类加载器与反射 |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Java-类加载器与反射" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Java-类加载器与反射
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/09/05/Java-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8%E4%B8%8E%E5%8F%8D%E5%B0%84/" class="article-date">
  <time datetime="2020-09-05T03:58:02.000Z" itemprop="datePublished">2020-09-05</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java/">Java</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.8k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">6分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="一、类加载器"><a href="#一、类加载器" class="headerlink" title="一、类加载器"></a>一、类加载器</h1><h2 id="1-1-类加载"><a href="#1-1-类加载" class="headerlink" title="1.1 类加载"></a>1.1 类加载</h2><p>当程序要使用某个类时，如果该类还未被加载到内存中，则系统会通过<strong>类的加载，类的连接，类的初始化</strong>这三个步骤来对类进行初始化。如果不出现意外情况，JVM将会连续完成这三个步骤，所以有时也把这三个步骤统称为<strong>类的加载或者类初始化</strong></p>
<h3 id="①类的加载"><a href="#①类的加载" class="headerlink" title="①类的加载"></a>①类的加载</h3><ul>
<li>就是指将class文件读入内存，并为之创建一个java.lang.Class对象</li>
<li>任何类被使用时，系统都会为之建立一个java.lang.Class对象</li>
</ul>
<h3 id="②类的连接"><a href="#②类的连接" class="headerlink" title="②类的连接"></a>②类的连接</h3><ul>
<li><strong>验证阶段</strong>：用于检验被加载的类是否有正确的内部结构，并和其他类协调一致</li>
<li><strong>准备阶段</strong>：负责为类变量分配内存，并设置默认初始化值</li>
<li><strong>解析阶段</strong>：将类的二进制数据中的符号引用替换为直接引用</li>
</ul>
<h3 id="③类的初始化"><a href="#③类的初始化" class="headerlink" title="③类的初始化"></a>③类的初始化</h3><ul>
<li>在该阶段，主要就是对类变量进行初始化</li>
</ul>
<h5 id="类的初始化步骤："><a href="#类的初始化步骤：" class="headerlink" title="类的初始化步骤："></a>类的初始化步骤：</h5><a id="more"></a>
<ul>
<li>假如类还未被加载和连接，则程序先加载并连接该类</li>
<li>假如该类的直接父类还未被初始化，则先初始化其直接父类</li>
<li>假如类中有初始化语句，则系统依次执行这些初始化语句</li>
</ul>
<p><strong>注意</strong>：第二步中系统对直接父类的初始化步骤也遵循这三个步骤</p>
<h5 id="类的初始化时机："><a href="#类的初始化时机：" class="headerlink" title="类的初始化时机："></a>类的初始化时机：</h5><ol>
<li>创建类的实例时</li>
<li>调用类的方法</li>
<li>访问类或者接口的类变量，或者为该类变量赋值</li>
<li>使用反射方式来强制创建某个类或接口对应的java.lang.Class对象</li>
<li>初始化某个类的子类</li>
<li>直接使用java.exe命令来运行某个主类</li>
</ol>
<h2 id="1-2-类加载器"><a href="#1-2-类加载器" class="headerlink" title="1.2 类加载器"></a>1.2 类加载器</h2><h3 id="①类加载器的作用"><a href="#①类加载器的作用" class="headerlink" title="①类加载器的作用"></a>①类加载器的作用</h3><ul>
<li>负责将.class文件加载到内存中，并为之生成对应的java.lang.Class对象</li>
<li>类加载机制可以帮助我们更好的理解程序的运行</li>
</ul>
<h3 id="②JVM的类加载机制"><a href="#②JVM的类加载机制" class="headerlink" title="②JVM的类加载机制"></a>②JVM的类加载机制</h3><ul>
<li><strong>全盘负责</strong>：就是当一个类加载器负责加载某个Class时，该Class所依赖的和引用的其他Class也将由该类加载器负责载入，除非显示使用另一个类加载器来载入</li>
<li><strong>父类委托</strong>：就是当一个类加载器负责加载某个Class时，先让父类加载器试图加载该Class,只有在父类加载器无法加载该类时才尝试从自己的类路径中加载该类</li>
<li><strong>缓存机制</strong>：保证所有加载过的Class都会被缓存，当程序需要使用某个Class对象时，类加载器先从缓存区中搜索该Class，只有当缓存区中不存在该Class对象时，系统才会读取该类对应的二进制数据，并将其转换成Class对象，存储到缓存区</li>
</ul>
<h3 id="③ClassLoader"><a href="#③ClassLoader" class="headerlink" title="③ClassLoader"></a>③ClassLoader</h3><p>ClassLoader是负责加载类的对象</p>
<p>java运行时具有以下内置类加载器</p>
<ul>
<li><p><strong>Bootstrap class loader</strong>：它是虚拟机的内置类加载器，通常表示为null，并且没有父null</p>
</li>
<li><p><strong>Platform class loader</strong>:平台类加载器可以看到所有平台类，平台类包括由平台类加载器或其祖先定义的JavaSE平台API，其实现类和JDK特定的运行时类</p>
</li>
<li><p><strong>System class loader</strong>:它也被称为应用程序类加载器，与平台类加载不同。系统类加载器通常用于定义引用程序类路径，模块路径和JDK特定工具上的类</p>
</li>
<li><p><strong>类加载器的继承关系：System的父加载器为Platform，而Platform的父类加载器为Bootstrap</strong></p>
</li>
</ul>
<p>ClassLoader中的两个方法</p>
<ul>
<li><strong>static ClassLoader getSystemClassLoader()</strong>:返回用于委托派的系统类加载器</li>
<li><strong>ClassLoader getParent()</strong>:返回父类加载器进行委派</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-31_18-33-21.jpg" alt=""></p>
<h1 id="二、反射"><a href="#二、反射" class="headerlink" title="二、反射"></a>二、反射</h1><h2 id="2-1-概述"><a href="#2-1-概述" class="headerlink" title="2.1 概述"></a>2.1 概述</h2><p><strong>Java反射机制</strong>：是指在运行时去获取一个类的变量和方法信息。然后通过获取到的信息来创建对象，调用方法的一种机制。由于这种动态性，可以极大的增强程序的灵活性，程序不用在编译期就完成确定，在运行期仍然可以扩展</p>
<h2 id="2-2-获取Class类的对象"><a href="#2-2-获取Class类的对象" class="headerlink" title="2.2 获取Class类的对象"></a>2.2 获取Class类的对象</h2><p>我们想要通过反射去使用一个类，首先我们要获取到该类的字节码文件对象，也就是类型为Class类型的对象</p>
<p>这里提供三种方式获取Class的对象</p>
<ol>
<li><strong>使用类的class属性</strong>来获取该类对应的Class对象</li>
<li><strong>调用对象的getClass()方法</strong>，返回该对象所属类对应的Class对象<br>（该方法是Object类中的方法，所有的Java对象都可以调用该方法）</li>
<li><strong>使用Class类中的静态方法forName(String className)</strong>,该方法需要传入字符串参数，该字符串参数的值是某个类的全路径，也就是完整包名的路径</li>
</ol>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-01_11-33-38.jpg" alt=""></p>
<h2 id="2-3-反射获取构造方法并使用"><a href="#2-3-反射获取构造方法并使用" class="headerlink" title="2.3 反射获取构造方法并使用"></a>2.3 反射获取构造方法并使用</h2><h3 id="Class类中用于获取构造方法的方法"><a href="#Class类中用于获取构造方法的方法" class="headerlink" title="Class类中用于获取构造方法的方法"></a>Class类中用于获取构造方法的方法</h3><ul>
<li>Constructor&lt;?&gt;[] <strong>getConstructors()</strong>:返回所有公共构造方法对象的数组</li>
<li>Constructor&lt;?&gt;[] <strong>getDeclaredConstructors()</strong>:返回所有构造方法对象的数组</li>
<li>Constructor<T> <strong>getConstructor(Class&lt;?&gt;…parameterTypes)</strong>:返回单个公共构造方法对象</li>
<li>Constructor<T> <strong>getDeclaredConstructor(Class&lt;?&gt;…parameterTypes)</strong>:返回单个构造方法对象        </li>
</ul>
<h3 id="Constructor类中用于创建对象的方法"><a href="#Constructor类中用于创建对象的方法" class="headerlink" title="Constructor类中用于创建对象的方法"></a>Constructor类中用于创建对象的方法</h3><ul>
<li><strong>T newInstance(Object…initargs)</strong>:根据指定的构造方法创建对象</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-01_14-49-07.jpg" alt=""></p>
<h2 id="2-4-反射获取构造方法并使用练习"><a href="#2-4-反射获取构造方法并使用练习" class="headerlink" title="2.4 反射获取构造方法并使用练习"></a>2.4 反射获取构造方法并使用练习</h2><h3 id="练习1-通过反射实现如下操作"><a href="#练习1-通过反射实现如下操作" class="headerlink" title="练习1 通过反射实现如下操作"></a>练习1 通过反射实现如下操作</h3><ul>
<li>Student s=new Student(“猪头新”,30,”西安”);</li>
<li>System.out.println(s)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> fan_she_2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Constructor;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationTargetException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ReflectDemo3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException </span>&#123;</span><br><span class="line">        <span class="comment">//获取Class对象</span></span><br><span class="line">        Class&lt;?&gt; c = Class.forName(<span class="string">"fan_she.Student"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//得到构造方法对象</span></span><br><span class="line">        Constructor&lt;?&gt; con = c.getConstructor(String<span class="class">.<span class="keyword">class</span>, <span class="title">int</span>.<span class="title">class</span>, <span class="title">String</span>.<span class="title">class</span>)</span>;</span><br><span class="line">        <span class="comment">//基本数据类型也可以通过.class得到对应的Class类型</span></span><br><span class="line"></span><br><span class="line">        Object obj = con.newInstance(<span class="string">"猪头新"</span>, <span class="number">20</span>, <span class="string">"西安"</span>);</span><br><span class="line">        System.out.println(obj);</span><br><span class="line">        <span class="comment">//Student&#123;name='猪头新', age=20, address='西安'&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="练习2-通过反射实现如下操作"><a href="#练习2-通过反射实现如下操作" class="headerlink" title="练习2 通过反射实现如下操作"></a>练习2 通过反射实现如下操作</h3><ul>
<li>Student s=new Student(“猪大头”);</li>
<li>System.out.println(S);</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-01_16-00-42.jpg" alt=""></p>
<p><strong>public void setAccessible(boolean flag):值为true，取消访问检查</strong></p>
<h2 id="2-5-反射获取成员变量并使用"><a href="#2-5-反射获取成员变量并使用" class="headerlink" title="2.5 反射获取成员变量并使用"></a>2.5 反射获取成员变量并使用</h2><p><strong>Class类中用于获取成员变量的方法</strong></p>
<ul>
<li>Field[] <strong>getFields()</strong>:返回所有公共成员变量对象的数组</li>
<li>Field[] <strong>getDeclaredFields()</strong>:返回所有成员变量对象的数组</li>
<li>Field <strong>getField(String name)</strong>:返回单个公共成员变量对象</li>
<li>Field <strong>getDeclaredField(String name)</strong>:返回单个成员变量对象</li>
</ul>
<p><strong>Field类中用于给成员变量赋值的方法</strong></p>
<ul>
<li>void <strong>set(Object obj,Object value)</strong>:给obj对象的成员变量赋值为value</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-05_11-24-45.jpg" alt=""></p>
<h2 id="2-6-反射获取成员方法并使用"><a href="#2-6-反射获取成员方法并使用" class="headerlink" title="2.6 反射获取成员方法并使用"></a>2.6 反射获取成员方法并使用</h2><p><strong>Class类中用于获取成员方法的方法</strong></p>
<ul>
<li>Method[] <strong>getMethods()</strong>:返回所有公共成员方法对象的数组，包括继承的</li>
<li>Method[] <strong>getDeclaredMethods()</strong>:返回所有成员方法对象的数组，不包括继承的</li>
<li>Method <strong>getMethod(String name,Class&lt;?&gt;…parameterTypes)</strong>:返回单个公共成员方法对象</li>
<li>Method <strong>getDeclaredMethod(String name,Class&lt;?&gt;…parameterTypes)</strong>:返回单个成员方法对象</li>
</ul>

      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8F%8D%E5%B0%84/" rel="tag">反射</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/09/06/JavaEE-JavaEE-Junit%E6%B5%8B%E8%AF%95%E3%80%81%E5%8F%8D%E5%B0%84%E4%B8%8E%E6%B3%A8%E8%A7%A3%E7%9A%84%E4%BD%BF%E7%94%A8/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JavaEE-Junit测试、反射与注解的使用
          
        </div>
      </a>
    
    
      <a href="/2020/08/30/Java-Stream%E6%B5%81/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Java-Stream流</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>